<template>
  <div class="page">
    <div class="title0">标题：南方东英</div>
    <van-button class="btnStart" type="primary" @click="toGame">开始游戏</van-button>
    <van-overlay :show="!store.state.bRead" @click="store.state.bRead = true" >
      <div class="popWelcome">
        免责声明，点击同意
      </div>
    </van-overlay>
    <van-popup round v-model:show="data.bPopBindID" :close-on-click-overlay="false">
      <div class="popBindID">
        <div class="_title">請輸入郵箱</div>
        <input class="_input" type="text" placeholder="請輸入郵箱" v-model.trim="data.strBindID">
        <van-button class="_button" type="primary" size="small" @click="bindID">绑定</van-button>
      </div>
    </van-popup>
  </div>
</template>

<script setup lang="ts">
import FingerprintJS from '@fingerprintjs/fingerprintjs'
import { onMounted, reactive } from 'vue';
import request from '../comm/request';
import moment from 'moment';
import { showToast } from 'vant';
import { useStore } from '../stores';
import router from '../router';
const store = useStore();

// 老代码
function getUrlParams(url:string) {
  const reg = /(\w+)=([^&]+)/g;
  const params = {} as any;
  let match;
  while ((match = reg.exec(url)) !== null) {
    params[match[1]] = match[2];
  }
  return params;
}
const urlParams = getUrlParams(location.href);
console.log('urlParams',urlParams);
//--end--
const data = reactive({
  // bPopWelcome : false,
  bPopBindID : false,
  strBindID : '',
})
onMounted(()=>{
  // data.bPopWelcome = true;
  checkLogin();
})
async function checkLogin(){
  if (!store.state.user) {
    // data.bShowTitle = true;
    toLogin();
    return;
  }
  let res = await request.get('/user/getScore')
  if (res.code == 1 && res.data) {
    const maxScore = res.data.score
    store.state.maxScore = maxScore;
  } else if (res.code == -1) {
    toLogin();
  }
}
async function toGame(){
  console.log(store.state);
  
  if (!store.state.user || !store.state.user.idcard) {
    await toLogin();
    if (data.bPopBindID) {
      return;
    }
  }
  getGameInfo()
}
// 获取活动详情
function getGameInfo () {
  console.log('xxxj 0');
  request.get(`/game/info/63`)
    .then((res: any) => {
    console.log('xxxj 1',res);
      if (res.code === 1) {
    console.log('xxxj 2');
        const data  = res.data
        if(data.status == 1) {
          console.log('router.push({ game');
          
          router.push({
            name: 'game',
            query: {
              id: '63'
            }
          })
        } else {
          data.startTimeStr = moment(data.starttime).format('YYYY年MM月DD日 HH:mm:ss')
          data.endtimeStr = moment(data.endtime).format('YYYY年MM月DD日 HH:mm:ss')
          showToast(`游戏时间${data.startTimeStr}--${data.endtimeStr}`)
        }
      }
    })
}
async function toLogin(){
  store.state.user = null;
  // this.$router.push({
  //   name: 'login',
  // })
  const fp = await FingerprintJS.load()
  const result = await fp.get()
  const visitorId = result.visitorId;
  console.log('visitorId',visitorId);
  // showToast(visitorId)
  if (visitorId) {
  // if (this.openid) {
    // req login
    const res = await request.post('/login',{},{params:{
      account: 'bxg1',
      password: 'bxg123',
      code: visitorId,
    }})
    if (res.code == 1 && res.data) {
      const user = res.data
      store.state.user = user;
      // 检查投顾id的逻辑先去掉
      // if (!user.idcard) {
      //   this.bBindID = true
      // } else {
      //   this.bBindID = false
      //   this.checkLogin();
      // }
      checkLogin();
    }
  }
}
async function bindID() {
    if (!data.strBindID) {
      return
    }
    let res = await request.post('/user/setIdCard?idCard='+data.strBindID)
    if (res.code == 1) {
      if (store.state.user) {
        store.state.user.investId = this.strBindID;
        showToast('绑定成功')
        data.bPopBindID = false;
      }
    } else {
      showToast(res.msg)
    }
}
</script>

<style lang="less" scoped>
.page{
  display: flex;
  flex-direction: column;
  align-items: center;
}
.test{
  width: 375px;
  background-color: wheat;
}
.popWelcome{
  width: 375px;
  height: 300px;
  background-color: orangered;
}
.btnStart{
  position: absolute;
  bottom: 80px;

}

.popBindID{
  width: 60vmin;
  height: min-content;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 5vmin 0;
  // ._title{
  //   font-size: 0.35rem;
  // }
  ._input{
    width: 80%;
    height: 8vmin;
    border: 1px solid #eee;
    border-radius: 1vmin;
    margin: 6vmin;
    text-align: center;
  }
  ._button {
    width: 20vmin;
  }
}
</style>